function f = cur2frac(p, d)
%CUR2FRAC Decimal currency values to fractional values.
%
%   F = cur2frac(P, D)
%
%   Inputs:
%   P - Scalar or vector of the decimal currency values.
%
%   D - Scalar or vector of the denominator of the fractions.
%
%   Outputs:
%   F - String or character array of the fractonal values.
%
%   Example:
%      >> f = cur2frac(12.125, 8)
%         f = 12.1
%
%      >> whos f
%        Name      Size                    Bytes  Class
%        f         1x4                         8  char array
%
%   See also FRAC2CUR, CUR2STR.

%       Copyright 1995-2006 The MathWorks, Inc.
%       $Revision: 1.6.2.3 $   $Date: 2006/06/16 20:09:30 $ 
 
% Input validation
if nargin < 2
    error('Finance:cur2frac:tooFewInputs', 'Too few inputs')
end

if isempty(p) || isempty(d)
    f = [];
    return
end

if ~isnumeric(p) || ~isnumeric(d)
    error('Finance:cur2frac:invalidInputClass', 'Inputs must be numeric.')
end

if numel(p) == 1
    p = p*ones(size(d));
end

if numel(d) == 1
    d = d*ones(size(p));
end

[ecode1, errMsg1] = checksiz([size(p); size(d)], mfilename);
if ecode1
    error('Finance:cur2frac:conflictingDataSizes', errMsg1)
end

% Algorithm
pnum = numel(p);
f = [];
for i = 1:pnum
    % Check for negative values
    if p(i) < 0
        coeff = -1;

    else
        coeff = 1;
    end

    frac = abs(round(rem(p(i), 1)*d(i)));
    if frac == d(i)
        crt = coeff;
        frac = 0;

    else
        crt = 0;
    end

    if frac < 10
        remain = fliplr(num2str(frac*10^floor(log10(d(i)))));

    else
        remain = num2str(frac);
    end

    sf =[num2str(coeff*floor(abs(p(i)))+crt), '.', remain];
    f = str2mat(f, sf);
end

f(1, :) = [];


% [EOF]
